package com.coc.controller.sysJobPo;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coc.core.Schedule.CronTaskRegistrar;
import com.coc.core.Schedule.SchedulingRunnable;
import com.coc.entity.core.SysJobPo;
import com.coc.service.sysJobPo.SysJobPoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.coc.entity.core.JsonResult;
import com.coc.entity.core.ResultTool;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import com.coc.core.utils.CheckObjField;
import com.coc.core.utils.StringRelaUtils;
import com.alibaba.fastjson.JSONObject;
/**
 * 定时任务实体类(SysJobPo)表控制层
 *
 * @author songju
 * @since 2020-02-24 19:35:00
 */
@RestController
@RequestMapping("sysJobPo")
public class SysJobPoController extends ApiController {
    /**
     * 服务对象
     */
    @Autowired
    private SysJobPoService sysJobPoService;
    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param sysJobPo 查询实体
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    public JsonResult selectAll(Page<SysJobPo> page, SysJobPo sysJobPo) {
        JsonResult result = ResultTool.success();
        QueryWrapper<SysJobPo> sqw = new CheckObjField<SysJobPo>().getFields(sysJobPo);
        result.setData(this.sysJobPoService.page(page, sqw));
        return result;
    }

    @PostMapping("/addJob")
    public JsonResult addJob(@RequestBody String json) {
        SysJobPo sysJobPo = JSONObject.parseObject(json, SysJobPo.class);
        sysJobPo.setCreateTime(new Date());
        sysJobPo.setUpdateTime(new Date());
        boolean save = sysJobPoService.save(sysJobPo);
        if (save){
            SchedulingRunnable task = new SchedulingRunnable(sysJobPo.getBeanName(), sysJobPo.getMethodName(), sysJobPo.getMethodParams());
            cronTaskRegistrar.addCronTask(task, sysJobPo.getCronExpression());
            return ResultTool.success();
        }else {
            return ResultTool.fail();
        }

    }
    //根据id批量删除
      @PostMapping("/delBatchByIds")
    public JsonResult delBatchByIds(@RequestBody String ids){
        JSONObject jsonObject = JSONObject.parseObject(ids);
        String idss = jsonObject.getString("id");
        List<String> ss = StringRelaUtils.strToList(idss);
          List<SysJobPo> sysJobPos = sysJobPoService.listByIds(ss);
        boolean b = sysJobPoService.removeByIds(ss);
          if (b){
              for (SysJobPo existedSysJob:sysJobPos){
                  SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                  cronTaskRegistrar.removeCronTask(task);
              }
            return ResultTool.success("删除成功");
        }else {
            return ResultTool.fail();
        }
    }
    @PostMapping("/editJob")
    public JsonResult editJob(@RequestBody String json) {
        SysJobPo sysJobPo = JSONObject.parseObject(json, SysJobPo.class);
        sysJobPo.setUpdateTime(new Date());
        SysJobPo existedSysJob = sysJobPoService.getById(sysJobPo.getId());
        boolean b = sysJobPoService.updateById(sysJobPo);
        if (b){
            if (sysJobPo.getJobStatus()==sysJobPo.getResJobStatus()){//状态是否改变
                if (sysJobPo.getJobStatus()==1){//改变之后是正常
                    SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                    cronTaskRegistrar.addCronTask(task, existedSysJob.getCronExpression());
                }else {//惊变之后是暂停
                    SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                    cronTaskRegistrar.removeCronTask(task);
                }
            }else {
                if (sysJobPo.getJobStatus()==0){
                    //移除
                    SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                    cronTaskRegistrar.removeCronTask(task);
                }else {
                    //先移除再添加
                    SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(), existedSysJob.getMethodParams());
                    cronTaskRegistrar.removeCronTask(task);
                    //添加
                    SchedulingRunnable task1 = new SchedulingRunnable(sysJobPo.getBeanName(), sysJobPo.getMethodName(), sysJobPo.getMethodParams());
                    cronTaskRegistrar.addCronTask(task1, sysJobPo.getCronExpression());
                }

            }
            return ResultTool.success();
        }else {
            return ResultTool.fail();
        }


    }
}